//
//  HomeHotCell.swift
//  KkmhSwift
//
//  Created by tenltrs on 2018/11/9.
//  Copyright © 2018 Tenltrs. All rights reserved.
//

import UIKit

class HomeHotCell: UITableViewCell,Reusable {
    
    /// 类别
    private var typeLabel : UILabel!
    
    ///全集label
    private var completeButton : UIButton!

    ///标题
    private var titleLabel : UILabel!
    
    /// 作者
    private var authorLabel : UILabel!
    
    /// 封面
    private var coverView : UIImageView!
    
    /// 评论
    private var commentsButton : UIButton!
    
    /// 点赞数
    private var likesButton : UIButton!
    
    /// 当前第几话
    private var subTitleLabel : UILabel!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        
        typeLabel = UIViewTool.mas_label(text: "", textColor: UIColor.white, textAligment: NSTextAlignment.center, fontSize: 12)
        typeLabel.layer.cornerRadius=2
        contentView.addSubview(typeLabel)
        typeLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(8)
        }
        
        
        completeButton=UIViewTool.mas_button(title: "全集", image: "ic_home_arrow_6x12_", backgroundImage: nil, titleColor: UIColor.BLACK9TEXTCOLOR, fontSize: 13).then({ (make) in
            make.ts_setImagePosition(TSImagePosition.right, spacing: 5)
        })
        contentView.addSubview(completeButton)
        completeButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalTo(contentView.snp.top).offset(55/2)
        }
        
        titleLabel=UIViewTool.mas_label(text: "", textColor: UIColor.BLACK3TEXTCOLOR, textAligment: NSTextAlignment.left, fontSize: 15)
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(typeLabel.snp.right).offset(10)
            make.centerY.equalTo(typeLabel)
            make.right.lessThanOrEqualTo(completeButton.snp.left).offset(-10)
        }

        authorLabel=UIViewTool.mas_label(text: "", textColor: UIColor.BLACK9TEXTCOLOR, textAligment: NSTextAlignment.left, fontSize: 12)
        contentView.addSubview(authorLabel)
        authorLabel.snp.makeConstraints { (make) in
            make.left.equalTo(typeLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(7)
            make.right.lessThanOrEqualTo(completeButton.snp.left).offset(-10)
        }
        
        let ratio = 750.0/420
        coverView = UIImageView()
        contentView.addSubview(coverView)
        coverView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(55)
            make.left.equalToSuperview()
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(coverView.snp.width).dividedBy(ratio)
        }
        
        let line = UIViewTool.mas_view(backColor: UIColor.BackgroundColor)
        contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.size.equalTo(CGSize.init(width: ScreenWidth, height: 5))
            make.top.equalTo(coverView.snp.bottom).offset(45)
        }
        
        commentsButton = UIViewTool.mas_button(title: nil, image: "ic_common_detail_normal_12x12_", backgroundImage: nil, titleColor: UIColor.BLACK9TEXTCOLOR, fontSize: 12).then({ (make) in
            make.ts_setImagePosition(TSImagePosition.left , spacing: 3)
        })
        contentView.addSubview(commentsButton)
        commentsButton.snp.makeConstraints { (make) in
            make.right.equalTo(completeButton)
            make.centerY.equalTo(coverView.snp.bottom).offset(45/2)
            make.height.equalTo(45)
        }
        
        likesButton = UIViewTool.mas_button(title: nil, image: "ic_common_praise_normal_12x12_", backgroundImage: nil, titleColor: UIColor.BLACK9TEXTCOLOR, fontSize: 12).then({ (make) in
            make.ts_setImagePosition(TSImagePosition.left , spacing: 3)
        })
        contentView.addSubview(likesButton)
        likesButton.snp.makeConstraints { (make) in
            make.right.equalTo(commentsButton.snp.left).offset(-10)
            make.centerY.equalTo(commentsButton)
            make.height.equalTo(45)
        }
        
        subTitleLabel=UIViewTool.mas_label(text: "", textColor: UIColor.BLACK5TEXTCOLOR, textAligment: NSTextAlignment.left, fontSize: 16)
        contentView.addSubview(subTitleLabel)
        subTitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(typeLabel)
            make.right.lessThanOrEqualTo(likesButton.snp.left).offset(-10)
            make.centerY.equalTo(likesButton)
        }
        
        self.hyb_lastViewInCell=line
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   
    func fillData(model : HomeAnimeModel, indexPath : IndexPath) {
        
        typeLabel.text=model.label_text
        typeLabel.textColor=UIColor(hexString: model.label_text_color!)
        typeLabel.layer.backgroundColor=UIColor(hexString: model.label_color!)?.cgColor
        
        titleLabel.text=model.topic?.title
        authorLabel.text = String(format: "作者：%@", model.topic?.user?.nickname ?? "")
        
//        coverView.sd_setImage(with: URL(string: model.cover_image_url ?? ""), placeholderImage: UIImage(named:"ic_new_comic_placeholder_s_355x149_"), options: SDWebImageOptions.refreshCached, progress: nil, completed: nil)
        coverView.ts_loadImage(url: model.cover_image_url, placeholderImage: "ic_new_comic_placeholder_s_355x149_")
        
        commentsButton.setTitle(model.comments_count?.stringValue, for: UIControl.State.normal)
        likesButton.setTitle(model.likes_count?.stringValue, for: UIControl.State.normal)
        
        subTitleLabel.text=model.title
    }
}
